<?php
declare (strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class SyncOrder extends Command {

    protected function configure() {
        // 指令配置
        $this->setName('sync_order')->setDescription('the sync_order command');
    }

    protected function execute(Input $input, Output $output) {
        $maxId = Db::name('sale_statistic')->max('old_id');
        $field = 'id as old_id,operator_id,franchiser_id,group_id,source_type,community_id,street_id,total_fee,cash_pay,
            online_pay,coupon_pay,refund_amount,created_at as create_time';
        // 扩展字段需要删掉
        $extend = $field . ',vip_discount,origin_total_fee';
        $list = Db::connect('cupboard')->table('order')->field($extend)->where('id', '>', $maxId)->limit(50)->select()->toArray();
        if (!$list) {
            $output->writeln('sync_order over');
            return;
        }
        $dataAll = [];
        foreach ($list as $key => $value) {
            if ($value['street_id']) {
                // 获取省市区ID
                $areaInfo = Db::connect('cupboard')->table('street')->where('id', $value['street_id'])->find();
                $provinceCode = $this->getProvinceCode($areaInfo['province']);
                $value['province_id'] = $provinceCode;

                $cityCode = $this->getCityCode($areaInfo['city'], $provinceCode);
                $value['city_id'] = $cityCode;
                $value['area_id'] = $this->getAreaCode($areaInfo['district'], $cityCode);
            } else {
                $value['province_id'] = 0;
                $value['city_id'] = 0;
                $value['area_id'] = 0;
            }
            $franchiserInfo = Db::connect('cupboard')->table('franchiser')->where('id', $value['franchiser_id'])->find();
            $value['contact'] = $franchiserInfo['contact'];
            $value['phone'] = $franchiserInfo['phone'];
            $value['franchiser_name'] = $franchiserInfo['name'];

            $value['vip_discount_amount'] = $value['vip_discount'] > 0 ? ($value['origin_total_fee'] * (100 - $value['vip_discount']) / 100) : 0;
            $value['product_sale_price'] = $value['origin_total_fee'] - $value['total_fee'];
            $value['no_pay'] = $value['total_fee'] - $value['online_pay'] - $value['cash_pay'];

            unset($value['vip_discount'], $value['origin_total_fee']);
            $dataAll[$key] = $value;
        }
        Db::name('sale_statistic')->insertAll($dataAll);
        $output->writeln('sync_order over');
    }

    /**
     * 根据名字获取省ID
     * @param $provinceName
     * @return int|mixed
     */
    protected function getProvinceCode($provinceName) {
        $provinceCode = Db::name('region')->where('name', $provinceName)->where('level', 1)->value('id');
        return $provinceCode ?: 0;
    }

    /**
     * 根据市名字和省ID 获取市ID
     * @param $cityName
     * @param $provinceCode
     * @return int|mixed
     */
    protected function getCityCode($cityName, $provinceCode) {
        if (!$provinceCode) {
            return 0;
        }
        $cityCode = Db::name('region')->where('name', $cityName)->where('pid', $provinceCode)->value('id');
        return $cityCode ?: 0;
    }

    /**
     * 根据区县名字和市ID 获取区县ID
     * @param $areaName
     * @param $cityCode
     * @return int|mixed
     */
    protected function getAreaCode($areaName, $cityCode) {
        if (!$cityCode) {
            return 0;
        }
        $areaCode = Db::name('region')->where('name', $areaName)->where('pid', $cityCode)->value('id');
        return $areaCode ?: 0;
    }
}
